﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Data.Linq;
using ECare.Client.templates;
using ECare.BUS.Data;
using ECare.BUS;
using ECare.Client.controls;
using ECare.Client.Services;


namespace ECare.Client
{
    public partial class frmWizard : frmNull
    {
        public frmWizard()
        {
            InitializeComponent();

            this.Load += frmWizard_Load;

            this.windowsUIButtonPanel1.ButtonClick += windowsUIButtonPanel1_ButtonClick;
        }


        public decimal TotalWeight { get; set; }



        void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.Button.Properties.Caption == "Previous")
            {
                this.btnPrevious_Click(null, null);
            }
            else if (e.Button.Properties.Caption == "Finish")
            {
                this.btnFinish_Click(null, null);
            }
            else if (e.Button.Properties.Caption == "Next")
            {
                this.btnNext_Click(null, null);
            }
            else if (e.Button.Properties.Caption == "Close")
            {
                this.btnClose_Click(null, null);
            }
            else
            {

            }

        }

        public string currentstep = "{0}/{1}";

        public int CurrentIndex { get; set; }

        public List<ucWizardInfo> steps = new List<ucWizardInfo>();
        public List<plan_detail_step_info> stepinfos = new List<plan_detail_step_info>();


        public PlanInfo currentPlan { get; set; }

        public int detail_Id { get; set; }
        public frmLoading frmloading { get; set; }
        void frmWizard_Load(object sender, EventArgs e)
        {


            var worker = new GetPlanDetailStepsHelper { currentPlan = this.currentPlan };
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();

            frmloading = new frmLoading();
            frmloading.ShowDialog();

            this.FormClosing += frmWizard_FormClosing;
        }

        void frmWizard_FormClosing(object sender, FormClosingEventArgs e)
        {
            //throw new NotImplementedException();
            if (!islaststep)
            {
                //只有不是最后一步的时候才暂停。
                if (MessageBox.Show("当前配料未完成,确认暂停?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) ==
                    DialogResult.No)
                {
                    e.Cancel = true;
                    return;

                }
                //save 
                PlanHelper.CancelPlanDetail(this.detail_Id, PlanStatus.pause);
            }
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //close the loading form
            frmloading.Close();
            if (e.Result == null)
            {
                MessageBox.Show("当前计划已经全部完成,或者有特殊情况存在,请查看计划状态", "ECare", MessageBoxButtons.OK);
                this.currentPlan.checkstatus = PlanStatus.done;// 全部完成
                this.Close();
                return;
            }
            stepinfos = e.Result as List<plan_detail_step_info>;
            //     this.lblText.Text = string.Format(currentstep, "1", stepinfos.Count * 2);

            int index = 0;
            if (!ApplicationData.IsAdjustment)
            {
                //如果系统中的校正没有执行，则第一步是矫正环节.
                var ucskin = new ucWizardSkin();
                //add the
                ucskin.Index = index;
                ucskin.LoadInformation();
                ucskin.Finished += ucresource_Finished;
                ucskin.ExcecuteNext += ucresource_ExcecuteNext;
                steps.Add(ucskin);
                index++;
            }


            foreach (var stepinfo in stepinfos)
            {

                detail_Id = stepinfo.plan_detail_id;

                //index++;            
                var ucresource = new ucWizardResources { currentstep = stepinfo, Index = index };
                ucresource.Finished += ucresource_Finished;

                ucresource.ExcecuteNext += ucresource_ExcecuteNext;
                ucresource.LoadInformation();



                steps.Add(ucresource);
                index++;
            }
            //the defaut value is show the 0 step.
            this.splitContainer1.Panel1.Controls.Clear();
            this.splitContainer1.Panel1.Controls.Add(steps[0]);
            steps[0].Dock = DockStyle.Fill;
            (steps[0] as IStep).LoadInformation();


            this.Text = string.Format("配料向导 {0}", detail_Id);
        }

        void ucresource_ExcecuteNext()
        {
            //执行下一步。
            this.btnNext_Click(null, null);

        }

        void ucresource_Finished(bool result)
        {
            if (result & this.islaststep)
            {
                MessageBox.Show("此次混料结束，混料向导将关闭.", "ECare", MessageBoxButtons.OK);
                //update the latest end time
                ResourcesHelper.UpdatePlanEndTime(this.detail_Id);


                var planstatus = PlanHelper.UpdatePlanStatus(this.currentPlan.plan_id);

                if (planstatus)
                {this.currentPlan.checkstatus = PlanStatus.done;
                }
                this.Close();
                return;
            }

            //if it is not the last step
            if (result && !this.islaststep)
            {
                //show the next step
                CurrentIndex = this.CurrentIndex + 1;
                if (CurrentIndex == this.steps.Count - 1)
                {
                    //the last step
                    //this.btnFinish.Enabled = true;
                    //this.btnNext.Enabled = false;
                    //this.btnPrevious.Enabled = true;

                    steps[CurrentIndex].IsFinished = true;
                    islaststep = true;
                    this.windowsUIButtonPanel1.Buttons["Finish"].Properties.Enabled = true;
                    this.windowsUIButtonPanel1.Buttons["Next"].Properties.Enabled = false;
                    this.windowsUIButtonPanel1.Buttons["Previous"].Properties.Enabled = true;

                }
                else
                {
                    //this.btnFinish.Enabled = false;
                    //this.btnNext.Enabled = true;
                    //this.btnPrevious.Enabled = true;

                    islaststep = false;
                    this.windowsUIButtonPanel1.Buttons["Finish"].Properties.Enabled = false;
                    this.windowsUIButtonPanel1.Buttons["Next"].Properties.Enabled = true;
                    this.windowsUIButtonPanel1.Buttons["Previous"].Properties.Enabled = true;

                }

                var nextstep = this.steps[CurrentIndex];
                this.splitContainer1.Panel1.Controls.Clear();
                this.splitContainer1.Panel1.Controls.Add(nextstep);

               
                nextstep.Dock = DockStyle.Fill;
                //      this.lblText.Text = string.Format(currentstep, CurrentIndex + 1, stepinfos.Count * 2);
            }
        }



        private void btnNext_Click(object sender, EventArgs e)
        {
            var step = this.splitContainer1.Panel1.Controls[0] as ucWizardInfo;
            if (step.ValidateInformation())
            {
                ResourcesHelper.UpdatePlanStartTime(this.detail_Id);
                bool executeresult = step.Execute();
            }
        }

        public bool islaststep { get; set; }
        private void btnFinish_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("这是最后一步，配料完成以后，这个配方的生成")
            islaststep = true;
            var step = this.splitContainer1.Panel1.Controls[0] as ucWizardInfo;
            if (step.ValidateInformation())
            {
                step.Execute();
            }
        }


        private void btnPrevious_Click(object sender, EventArgs e)
        {


            var step = this.splitContainer1.Panel1.Controls[0] as ucWizardInfo;
            CurrentIndex = this.CurrentIndex - 1;
            if (CurrentIndex == 0)
            {
                //this.btnFinish.Enabled = false;
                //this.btnNext.Enabled = true;
                //this.btnPrevious.Enabled = false;


                islaststep = true;
                this.windowsUIButtonPanel1.Buttons["Finish"].Properties.Enabled = false;
                this.windowsUIButtonPanel1.Buttons["Next"].Properties.Enabled = true;
                this.windowsUIButtonPanel1.Buttons["Previous"].Properties.Enabled = false;

            }
            else
            {
                //this.btnFinish.Enabled = false;
                //this.btnNext.Enabled = true;
                //this.btnPrevious.Enabled = true;



                this.windowsUIButtonPanel1.Buttons["Finish"].Properties.Enabled = false;
                this.windowsUIButtonPanel1.Buttons["Next"].Properties.Enabled = true;
                this.windowsUIButtonPanel1.Buttons["Previous"].Properties.Enabled = true;

            }

            var laststep = this.steps[CurrentIndex];
            this.splitContainer1.Panel1.Controls.Clear();
            this.splitContainer1.Panel1.Controls.Add(laststep);
            islaststep = false;

            laststep.Dock = DockStyle.Fill;
            //     this.lblText.Text = string.Format(currentstep, CurrentIndex + 1, stepinfos.Count * 2);


        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }




    }
}
